package com.gqj.meter.controller;

import com.alibaba.fastjson.JSON;
import com.gqj.common.annotation.Log;
import com.gqj.common.constant.UserConstants;
import com.gqj.common.core.controller.BaseController;
import com.gqj.common.core.domain.AjaxResult;
import com.gqj.common.core.page.TableDataInfo;
import com.gqj.common.enums.BusinessType;
import com.gqj.common.utils.StorageUtil;
import com.gqj.common.utils.poi.ExcelUtil;
import com.gqj.meter.domain.MdWhAreaInfo;
import com.gqj.meter.domain.vo.MdStockPosInfoVo;
import com.gqj.meter.service.IMdWhAreaInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 库区信息Controller
 *
 * @author lxq
 * @date lxq-01-09
 */
@Api(value = "/meter/mdWhAreaInfo", description = "库区信息管理")
@RestController
@RequestMapping("/meter/mdWhAreaInfo")
public class MdWhAreaInfoController extends BaseController {
    @Autowired
    private IMdWhAreaInfoService mdWhAreaInfoService;
    @Value("${downloadPath}")
    private String downloadPath;

    /**
     * 查询库区信息列表
     */
    @ApiOperation("查询库区信息列表")
    @PreAuthorize("@ss.hasPermi('storage:mdWhAreaInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(MdWhAreaInfo mdWhAreaInfo) {
        startPage();
        List<MdWhAreaInfo> list = mdWhAreaInfoService.selectMdWhAreaInfoList(mdWhAreaInfo);
        return getDataTable(list);
    }

    /**
     * 导出库区信息列表
     */
    @ApiOperation("导出库区信息列表")
    @PreAuthorize("@ss.hasPermi('storage:mdWhAreaInfo:export')")
    @Log(title = "库区信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MdWhAreaInfo mdWhAreaInfo) {
        List<MdWhAreaInfo> list = mdWhAreaInfoService.selectMdWhAreaInfoList(mdWhAreaInfo);
        ExcelUtil<MdWhAreaInfo> util = new ExcelUtil<MdWhAreaInfo>(MdWhAreaInfo.class);
        util.exportExcel(response, list, "库区信息数据");
    }

    /**
     * 获取库区信息详细信息
     */
    @ApiOperation("获取库区信息详细信息")
    @PreAuthorize("@ss.hasPermi('storage:mdWhAreaInfo:query')")
    @GetMapping(value = "/{whAreaId}")
    public AjaxResult getInfo(@PathVariable("whAreaId") Long whAreaId) {
        return success(mdWhAreaInfoService.selectMdWhAreaInfoByWhAreaId(whAreaId));
    }

    /**
     * 新增库区信息
     */
    @ApiOperation("新增库区信息")
    @PreAuthorize("@ss.hasPermi('storage:mdWhAreaInfo:add')")
    @Log(title = "库区信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MdWhAreaInfo mdWhAreaInfo) {
        if (null == mdWhAreaInfo.getWhAreaId() || 0 == mdWhAreaInfo.getWhAreaId()) {
            return AjaxResult.error("新增失败，库区ID为空！");
        }
        MdWhAreaInfo tempEntity = mdWhAreaInfoService.selectMdWhAreaInfoByWhAreaId(mdWhAreaInfo.getWhAreaId());
        if (null != tempEntity) {
            return AjaxResult.error("新增'" + mdWhAreaInfo.getName() + "'失败，库区ID已存在！");
        }
        if (UserConstants.NOT_UNIQUE.equals(mdWhAreaInfoService.checkWhAreaNoUnique(mdWhAreaInfo))) {
            return AjaxResult.error("新增'" + mdWhAreaInfo.getName() + "'失败，库区编号已存在！");
        }

        return toAjax(mdWhAreaInfoService.insertMdWhAreaInfo(mdWhAreaInfo));
    }

    /**
     * 修改库区信息
     */
    @ApiOperation("修改库区信息")
    @PreAuthorize("@ss.hasPermi('storage:mdWhAreaInfo:edit')")
    @Log(title = "库区信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MdWhAreaInfo mdWhAreaInfo) {
        if (null == mdWhAreaInfo.getWhAreaId() || 0 == mdWhAreaInfo.getWhAreaId()) {
            return AjaxResult.error("修改失败，库区ID为空！");
        }
        if (UserConstants.NOT_UNIQUE.equals(mdWhAreaInfoService.checkWhAreaNoUnique(mdWhAreaInfo))) {
            return AjaxResult.error("修改'" + mdWhAreaInfo.getName() + "'失败，库区编号已存在！");
        }
        return toAjax(mdWhAreaInfoService.updateMdWhAreaInfo(mdWhAreaInfo));
    }

    /**
     * 删除库区信息
     */
    @ApiOperation("删除库区信息")
    @PreAuthorize("@ss.hasPermi('storage:mdWhAreaInfo:remove')")
    @Log(title = "库区信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{whAreaIds}")
    public AjaxResult remove(@PathVariable Long[] whAreaIds) {
        return toAjax(mdWhAreaInfoService.deleteMdWhAreaInfoByWhAreaIds(whAreaIds));
    }

    /**
     * 查询库区下拉框
     */
    @ApiOperation("查询库区下拉框")
    @PreAuthorize("@ss.hasPermi('storage:mdWhAreaInfo:mdWhAreaInfoOptions')")
    @GetMapping("/mdWhAreaInfoOptions")
    public AjaxResult mdWhAreaInfoOptions(MdWhAreaInfo mdWhAreaInfo) {
        List<Map<String, Object>> list = mdWhAreaInfoService.mdWhAreaInfoOptions(mdWhAreaInfo);
        return AjaxResult.success(list);
    }

    /**
     * 下载库区json文件
     */
    @ApiOperation("下载库区json文件")
    @PreAuthorize("@ss.hasPermi('storage:mdWhAreaInfo:downloadJsonMdWhAreaInfo')")
    @GetMapping("/downloadJsonMdWhAreaInfo")
    public AjaxResult downloadJsonMdWhAreaInfo(MdWhAreaInfo mdWhAreaInfo) {
        List<MdWhAreaInfo> wai = mdWhAreaInfoService.selectMdWhAreaInfoList(mdWhAreaInfo);
        List<MdStockPosInfoVo> vos = StorageUtil.copyList(wai, MdStockPosInfoVo::new);

        String orderNo = StorageUtil.getOrderNoTo12();
        // 生成文件名
        String fileName = downloadPath;
        String jsonStringAc = JSON.toJSONString(vos);
        StorageUtil.createJsonFile(jsonStringAc, fileName, "库区数据" + orderNo);
        return AjaxResult.success(fileName + "库区数据" + orderNo);
    }

    @ApiOperation("下载库区模板")
    @PreAuthorize("@ss.hasPermi('storage:mdWhAreaInfo:importTemplateMdWhAreaInfo')")
    @PostMapping("/importTemplateMdWhAreaInfo")
    public void importTemplateMdWhAreaInfo(HttpServletResponse response) {
        ExcelUtil<MdWhAreaInfo> util = new ExcelUtil<MdWhAreaInfo>(MdWhAreaInfo.class);
        util.importTemplateExcel(response, "库区数据");
    }

    /**
     * 导入库区execl文件
     */
    @ApiOperation("导入库区execl文件")
    @Log(title = "导入库区xecl文件", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('storage:mdWhAreaInfo:importDataMdWhAreaInfo')")
    @PostMapping("/importDataMdWhAreaInfo")
    public AjaxResult importDataMdWhAreaInfo(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<MdWhAreaInfo> util = new ExcelUtil<MdWhAreaInfo>(MdWhAreaInfo.class);
        List<MdWhAreaInfo> mdWhAreaInfoList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = mdWhAreaInfoService.importMdWhAreaInfo(mdWhAreaInfoList, updateSupport, operName);
        return success(message);
    }

}
